import {savePageConfigure,getColumnConfig} from '../../services/configColum/index'
import {openNotice, isNull, User} from '../../utils'
import pathToRegexp from 'path-to-regexp'

export default {
  namespace: 'configColumAdd',
  state: {
    columnConfigId: 0,
    columnConfigName:'',
    detailLoading: true,
    columnConfig:[] // 栏目内容的配置
  },
  subscriptions: {
    setup({dispatch, history}, done) {
      const unlisten = history.listen(function (location) {
        const match = pathToRegexp('/configColumAdd/:id/:name').exec(location.pathname);
        dispatch({ // 获取首页配置信息
          type: 'setInit',
        });
        if (match) {
          dispatch({ // 获取首页配置信息
            type: 'getColumnsConfig',
            payload: {
              id:match[1],
              columnConfigName:match[2]
            }
          });
        }
      });
      return unlisten;
    }
  },
  effects: {
    // 保存菜单配置信息
    *add({payload}, {call, put}){
      const data = yield call(savePageConfigure,payload);
      if (data.status == 200) {
        openNotice('success', '提示', "保存成功");
      }else {
        openNotice('error', '保存失败', data.msg);
      }
    },
    *managerTableItem({payload},{select,put}){
      yield put({
        type: 'managerFilters',
        payload
      });
    },
    // 处理类型
    *handleType({payload},{select,put}){
      yield put({
        type: 'setHandleType',
        payload
      });
    },
    // 得到栏目内容配置
    *getColumnsConfig({payload}, {call, put}){
      const data = yield call(getColumnConfig, payload);
      if (data.status == 200 && data.data) {
        yield put({
          type: 'setColumnConfig',
          payload: {
            columnConfig: data.data.components
          }
        });
      }
      yield put({
        type: 'setColumnConfigId',
        payload: {
          columnConfigName:payload.columnConfigName,
          columnConfigId: payload.id
        }
      });
    },
    // 模板操作
    *itemHandle({payload},{select,put}){
      yield put({
        type: 'setaddHandle',
        payload
      });
    },
  },
  reducers: {
    setInit(state, action){
      return {...state, columnConfig:[]}
    },
    setColumnConfig(state, action){
      return {...state, ...action.payload}
    },
    setColumnConfigId(state, action){
      return {...state, ...action.payload}
    },
    setaddHandle(state, action){
      var columnConfig =  state.columnConfig;
         if(action.payload.type=='add'){
           columnConfig.push({
             "title": "",
             "type": 2,
             "inputFiled": "",
             "buttons": [
               {
                 "butttonName": "",
                 "action": ""
               }
             ]
           });
         }else {
            columnConfig.splice(action.payload.index, 1);
         }
      return {...state, ...columnConfig}
    },

    setHandleType(state, action){
      var columnConfig =  state.columnConfig;
          columnConfig[action.payload.index].type = action.payload.type;
          if(action.payload.type=="1"){
            if(!columnConfig[action.payload.index].tableHeads){
              columnConfig[action.payload.index].tableHeads= [];
              columnConfig[action.payload.index].tableHeads.push({displayName : '', fieldName:''})
            }
          }
          return {...state, ...columnConfig}
    },
    hideDetailLoading(state){
      return {...state, detailLoading: false}
    },
    managerFilters(state,action){
      var columnConfig =  state.columnConfig;
      switch (action.payload.type){
        case 'add' :{
          columnConfig[action.payload.index].tableHeads.splice(action.payload.idx+1,0,{displayName : '', fieldName:''});
          break ;
        }
        case 'delete' :{
          columnConfig[action.payload.index].tableHeads.splice(action.payload.idx, 1);
          break ;
        }
      };
      return {...state, ...columnConfig}
    }
  }
}
